#include "../collections.h"

#include <stdlib.h>

stack stack_create()
{
	stack self = malloc(sizeof(struct stack));
	self->top = NULL;
	return self;
}

void stack_delete(stack self)
{
	stack_clear(self);
	free(self);
	self = NULL;
}

void stack_clear(stack self)
{
	while (!stack_isempty(self))
		_stack_removetop(self);
}

int stack_isempty(stack self)
{
	return self->top == NULL;
}

void _stack_push(stack self, int T, void * value)
{
	self->top = node_create(T, value, self->top);
}

int _stack_pop(stack self, int T, void * value)
{
	if (stack_isempty(self))
		return 0;

	_stack_peek(self, T, value);
	_stack_removetop(self);
	return 1;
}

void _stack_peek(stack self, int T, void * value)
{
	node_read(self->top, T, value);
}

void _stack_removetop(stack self)
{
	node aux = self->top;
	self->top = node_getlink(aux);
	node_delete(aux);
}
